package threadLocal;

/**
 * User: cool
 * Date: 2017/12/12
 * Time: 14:48
 */
public class TestNum {
    //private  int threadLocalNum = 0;
    private  static  ThreadLocal<Integer> threadLocalNum = new ThreadLocal<Integer>(){
        @Override
        protected Integer initialValue() {
            return 0;
        }
    };

    public int getNextNum(){
        threadLocalNum.set(threadLocalNum.get()+1);
        return threadLocalNum.get();
        //return threadLocalNum ++;
    }

    public static void main(String[] args){
        TestNum testNum = new TestNum();
        TestClinet t1  = new TestClinet(testNum);
        TestClinet t2  = new TestClinet(testNum);
        TestClinet t3  = new TestClinet(testNum);
        t1.start();
        t2.start();
        t3.start();

    }

    private static class TestClinet extends Thread {
        TestNum testNum = null;

        public TestClinet(TestNum testNum){
            this.testNum = testNum;
        }

        @Override
        public void run() {
            for (int i = 0; i < 3; i++) {
                System.out.println("thread[" + Thread.currentThread().getName()+"]"+ testNum.getNextNum());
            }
        }
    }
}
